//
//  ViewControllerForVideoList.m
//  世界
//
//  Created by admin on 16/2/14.
//  Copyright © 2016年 admin. All rights reserved.
//

#import "ViewControllerForVideoList.h"
#import <MJRefresh/MJRefresh.h>
#import <UIImageView+WebCache.h>
#import "CellForVideoList.h"
#import "ViewControllerForVideoPlay.h"
#import "ModelForForVideoCollect.h"
#import "DataBaseForVideo.h"
#import "NetworkingHandler.h"

@interface ViewControllerForVideoList ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, retain) UITableView *tabView;
@property (nonatomic, retain) NSMutableArray *arrTitle;
@property (nonatomic, retain) NSMutableArray *arrDescription;
@property (nonatomic, retain) NSMutableArray *arrCover;
@property (nonatomic, retain) NSMutableArray *arrMp4_url;
@property (nonatomic, retain) NSString *strWithURL;
@property (nonatomic, retain) NSArray *arrForKey;
@property (nonatomic, assign) NSInteger number;
@property (nonatomic, retain) NSMutableArray *arrLength;
@property (nonatomic, retain) NSMutableArray *arrPlayCount;
@property (nonatomic, retain) NSMutableArray *arrReplyCount;
@property (nonatomic, retain) NSMutableArray *arrVid;
@property (nonatomic, retain) NSMutableArray *arrForCollect;
@property (nonatomic, assign) BOOL isSelect;

@end

@implementation ViewControllerForVideoList

#pragma mark 重写dealloc方法
- (void)dealloc
{
    [_strForPresentItem release];
    
    [_arrTitle release];
    
    [_arrDescription release];
    
    [_arrCover release];
    
    [_arrMp4_url release];
    
    [_strWithURL release];
    
    [_arrForKey release];
    
    [_arrLength release];
    
    [_arrPlayCount release];
    
    [_arrReplyCount release];
    
    [_arrVid release];
    
    [_arrForCollect release];
    
    [super dealloc];
}





#pragma mark 视图的生命周期 >>>>>>
#pragma mark 视图加载完成
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.number = 0;
    
    self.arrTitle = [NSMutableArray array];
    
    self.arrCover = [NSMutableArray array];
    
    self.arrMp4_url = [NSMutableArray array];
    
    self.arrDescription = [NSMutableArray array];
    
    self.arrLength = [NSMutableArray array];
    
    self.arrPlayCount = [NSMutableArray array];
    
    self.arrReplyCount = [NSMutableArray array];
    
    self.arrVid = [NSMutableArray array];
    
    self.arrForCollect = [NSMutableArray array];
    
    [self handleData];
    
    [self createTabView];
    
    self.tabView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        // 进入刷新状态后会自动调用这个block
        [self handleData];
    }];
}



#pragma mark 接收到内存警告!!!
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}




#pragma mark 自定义方法 >>>>>>
#pragma mark 解析数据
- (void)handleData
{
    //设置访问地址
    switch ([self.strForPresentItem intValue])
    {
        case 0:
            self.strWithURL = [NSString stringWithFormat:@"http://c.3g.163.com/nc/video/list/VAP4BFE3U/y/%ld-10.html", self.number * 10];
            break;
            
        case 1:
            self.strWithURL = [NSString stringWithFormat:@"http://c.3g.163.com/nc/video/list/VAP4BFR16/y/%ld-10.html", self.number * 10];
            break;
            
        case 2:
            self.strWithURL = [NSString stringWithFormat:@"http://c.3g.163.com/nc/video/list/VAP4BG6DL/y/%ld-10.html", self.number * 10];
            break;
            
        case 3:
            self.strWithURL = [NSString stringWithFormat:@"http://c.3g.163.com/nc/video/list/VAP4BGTVD/y/%ld-10.html", self.number * 10];
            break;
    }
    
    self.number ++;
    
    [NetworkingHandler netWorkingWithURL:self.strWithURL completeHandle:^(NSURLResponse *response, NSData *data ,id result) {
        
        switch ([self.strForPresentItem intValue])
        {
            case 0:
                self.arrForKey = [result objectForKey:@"VAP4BFE3U"];
                break;
                
            case 1:
                self.arrForKey = [result objectForKey:@"VAP4BFR16"];
                break;
                
            case 2:
                self.arrForKey = [result objectForKey:@"VAP4BG6DL"];
                break;
                
            case 3:
                self.arrForKey = [result objectForKey:@"VAP4BGTVD"];
                break;
                
            default:
                break;
        }
        
        for (NSDictionary *dic in self.arrForKey)
        {
            
            ModelForForVideoCollect *model = [[ModelForForVideoCollect alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.arrForCollect addObject:model];
            
            [model release];
            
            NSString *str1 = [dic objectForKey:@"title"];
            
            NSString *str2 = [dic objectForKey:@"description"];
            
            NSString *str3 = [dic objectForKey:@"cover"];
            
            NSString *str4 = [dic objectForKey:@"mp4_url"];
            
            NSString *str5 = [dic objectForKey:@"length"];
            
            NSString *str6 = [dic objectForKey:@"playCount"];
            
            NSString *str7 = [dic objectForKey:@"replyCount"];
            
            NSString *str8 = [dic objectForKey:@"vid"];
            
            [self.arrTitle addObject:str1];
            
            [self.arrDescription addObject:str2];
            
            [self.arrCover addObject:str3];
            
            [self.arrMp4_url addObject:str4];
            
            [self.arrLength addObject:str5];
            
            [self.arrPlayCount addObject:str6];
            
            [self.arrReplyCount addObject:str7];
            
            [self.arrVid addObject:str8];
        }
    
            
            [self.tabView reloadData];
            
            
            [self.tabView.mj_footer endRefreshing];
        
    }];
}




#pragma mark 创建TabView
- (void)createTabView
{
    self.tabView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    
    [self.view addSubview:self.tabView];
    
    self.tabView.dataSource = self;
    
    self.tabView.delegate = self;
    
    [self.tabView registerClass:[CellForVideoList class] forCellReuseIdentifier:@"pool1"];
    
    //去掉分割线
    self.tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
}



#pragma mark UITableViewDataSource 协议方法 >>>>>>
#pragma mark 相应分区的行的个数 (required 1/2)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrMp4_url.count;
}



#pragma mark 给每行添加cell (required 2/2)
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    CellForVideoList *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
    
    //去掉选择cell之后背景颜色灰的效果
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    
    
    
    cell.arrForCollect = self.arrForCollect;
    
    cell.number = indexPath.row;
    
    ModelForForVideoCollect *model2 = self.arrForCollect[indexPath.row];
    
    self.isSelect =  [[DataBaseForVideo shareDataBaseHandle]select:model2];
    if (self.isSelect)
    {
        [cell.buttonForCollect setBackgroundImage:[UIImage imageNamed:@"收藏"] forState:UIControlStateNormal];
    }
    else
    {
        [cell.buttonForCollect setBackgroundImage:[UIImage imageNamed:@"取消收藏"] forState:UIControlStateNormal];
    }

    
    
    
    
    
    NSString *timeLength = self.arrLength[indexPath.row];
    
    cell.lableForTimeLength.text = [[NSString alloc] initWithFormat:@"%02d:%02d", timeLength.intValue / 60, timeLength.intValue % 60];
    
    NSString *playCount = self.arrPlayCount[indexPath.row];
    
    if (playCount.intValue >= 10000)
    {
        cell.lableForPlayCount.text = [NSString stringWithFormat:@"%.1f万", playCount.floatValue / 10000];
    }
    else
    {
        cell.lableForPlayCount.text = [NSString stringWithFormat:@"%d", playCount.intValue];
    }

    NSString *replyCount = self.arrReplyCount[indexPath.row];
    
    cell.lableForReplyCount.text = [NSString stringWithFormat:@"%d", replyCount.intValue];
    
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:self.arrCover[indexPath.row]]];
    
    cell.lableForTitle.text = [self.arrTitle objectAtIndex:indexPath.row];
    
    cell.lableForDescription.text = [self.arrDescription objectAtIndex:indexPath.row];
    
    return cell;
}




#pragma mark UITableViewDelegate 协议方法 >>>>>>
#pragma mark 设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 310;
}



#pragma mark 选择相应的行所触发的方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ViewControllerForVideoPlay *videoView = [[ViewControllerForVideoPlay alloc] init];
    
    videoView.mp4_url = [self.arrMp4_url objectAtIndex:indexPath.row];
    
    videoView.vid = self.arrVid[indexPath.row];
    
    //模态推出效果 (翻页)
    //videoView.modalTransitionStyle = UIModalTransitionStylePartialCurl;
    
    [self presentViewController:videoView animated:YES completion:^{
        
        videoView.lableForTitle.text = [self.arrTitle objectAtIndex:indexPath.row];
        
    }];
    
     videoView.lableForTitle.text = [self.arrTitle objectAtIndex:indexPath.row];
    
    
}

@end
